<template>
	<vCommonPage :type="1" :title="$route.query.title">
		<div slot="customContent" style="height: 100%;">
			<Sfz v-if="$route.query.code == 'sfz' && !zzjdShow" @ReadedIdInfo="ReadedIdInfo"></Sfz>
			<Ynjzk v-if="$route.query.code == 'ynjzk'&& !zzjdShow" @ReadedYnjzkInfo="ReadedYnjzkInfo"></Ynjzk>
			<Ybk v-if="$route.query.code == 'ybk'&& !zzjdShow" @ReadedYbkInfo="ReadedYbkInfo"></Ybk>
			<Dzjzk v-if="$route.query.code == 'dzjkk'&& !zzjdShow" @ReadedDzjzkInfo="ReadedDzjzkInfo"></Dzjzk>
			<smsb v-if="$route.query.code == 'smsb'&& !zzjdShow" @ReadedSaoMaInfo="ReadedSaoMaInfo"></smsb>
			<zzjd v-if="zzjdShow" @gotoNextPage='gotoNextPage'></zzjd>
			<vTip v-if="tipText" :tipText="tipText"></vTip>
		</div>
	</vCommonPage>
</template>
<script>
	import Sfz from './sfz'
	import Ynjzk from './ynjzk.vue'
	import Ybk from './ybk.vue'
	import Dzjzk from './dzjzk.vue'
	import smsb from './smsb.vue'
	import zzjd from './zzjd.vue'
	import context from '../../main.js'
	import {
		MessageBox
	} from "element-ui";
	import {
		getUserInfo,
		getInpatientUnRecordList
	} from "@api/apiBox/user"
	import {
		changeChineToDate
	} from "../../assets/js/getTime.js"
	export default {
		name: "readCardCommon",
		data() {
			return {
				fullscreenLoading: false,
				//自助建档是否显示
				zzjdShow: false,
				tipText: ''
			};
		},
		methods: {
			//身份证读卡回调
			async ReadedIdInfo() {
				//前往自助建档
				// this.zzjdShow = true;
				//532231195010272549   532231194308240721  2201110154
				let code = "";
				console.log('this.$global.isDevlopment', this.$global.isDevlopment)
				if (this.$global.isDevlopment) {
					code = this.$store.state.user.userIdCardInfo.ID ? this.$store.state.user.userIdCardInfo.ID :
						'110101199003078873';
				} else {
					code = this.$store.state.user.userIdCardInfo.ID;
				}
				console.log(code)
				let data = {
					data: {
						code: code, //身份证号/有效的证件证号
						codeType: 'sfz', //身份证类型
						hospitalId: this.$store.state.terminfo.hospId
					},
					that: this
				}
				const res = await getUserInfo(data);
				console.log(res.data, '---')
				this.$store.dispatch("appointment/updatecardNo", code);
				if (res.data) {
					//将用户信息存入
					this.$store.dispatch("user/updateuserinfo", res.data);
					if (this.$route.query.nextPage == 'yjjcz') {
						//通过patintId去查询住院号
						let formData = {
							data: {
								patientId: this.$store.state.user.userinfo.patientId, //患者ID
							},
							that: this
						}
						const zyInfo = await getInpatientUnRecordList(formData);
						if (zyInfo.data.length > 0) {
							let inPatientId = zyInfo.data[0].inPatientId;
							//通过住院ID去查询患者身份信息
							let inNewData = {
								data: {
									code: inPatientId,
									codeType: "zyh",
									hospitalId: this.$store.state.terminfo.hospId
								},
								that: this
							}
							const res = await getUserInfo(inNewData);
							if (!res.data) {
								context.$alert('', '查无数据！', {})
								let _this = this;
								setTimeout(() => {
									_this.tipText = '';
								}, 1000)
							} else {
								this.$store.dispatch("user/updateuserinfo", res.data);
								//前往下一个页面
								this.gotoNextPage();
							}
						} else {
							context.$alert('', '该身份证无住院信息！', {})
						}
						console.log('zyInfo', zyInfo.data)
					} else {

						this.gotoNextPage();
					}
				} else {
					//机器读取身份证回调 前往自助建档
					this.zzjdShow = true;
				}
			},
			//医疗就诊卡读卡回调
			async ReadedYnjzkInfo() {
				this.readCardError();
				// this.gotoNextPage();
			},
			//医保卡读卡回调
			async ReadedYbkInfo() {
				this.readCardError();
				// this.gotoNextPage();
			},
			//电子健康卡读卡回调
			async ReadedDzjzkInfo() {
				this.readCardError();
				// this.gotoNextPage();
			},
			//二维码扫描
			async ReadedSaoMaInfo() {
				this.readCardError();
				// this.gotoNextPage();
			},
			//前往下一个页面
			gotoNextPage() {
				console.log(this.$route.query.nextPage)
				if (this.$route.query.nextPage == 'mzgh') {
					//今天日期
					let toDay = this.$getTime.changeChineToDate(new Date(), 'YYYY-MM-DD');
					//将日期存缓存中
					this.$store.dispatch("appointment/updateDate", toDay); //预约日期
					this.$router.push({
						name: 'mzyy',
						query: {
							code: 'mzgh'
						}
					})
				} else {
					this.$router.push({
						name: this.$route.query.nextPage,
						query: {
							code: this.$route.query.nextPage
						}
					})
				}
			},
			readCardError() {
				context.$alert('', '读卡失败！暂时没有接口：', {
					confirmButtonText: '确定',
					callback: action => {
						if (action === 'confirm') {
							context.$router.push({
								name: "homepage"
							});
						}
					}
				});
			}
		},
		computed: {},
		components: {
			Sfz,
			Ynjzk,
			Ybk,
			Dzjzk,
			smsb,
			zzjd
		},
		mounted() {},
		created() {

		}
	};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
	@import "../../assets/css/skzs.scss";
</style>
